package com.cyc.basic.leecode.排序;

/**
 * @author chenyunchang
 * @title
 * @date 2021/3/30 14:06
 * @Description:
 */
public class ArrayTest {
    /**
     * 整体思想: 由于上下每组都已经排好序, 则只需比较中间线的数据, 然后根据大小再次进行对角线比较
     */

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        int[][] arr = {
                {1, 4, 7, 11, 15},
                {2, 5, 8, 12, 19},
                {3, 6, 9, 16, 22},
                {10, 13, 14, 17, 24},
                {18, 21, 23, 26, 30}
        };
        Solution solution = new Solution();
        boolean numberIn2DArray = solution.findNumberIn2DArray(arr, 20);
        System.out.println(numberIn2DArray);
    }
}

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int m, n;
        if (matrix == null || (m = matrix.length) == 0 || matrix[0] == null || (n = matrix[0].length) == 0) {
            return false;
        }
        int i = 0, j = n - 1;
        while (i < m && j >= 0) {
            if (matrix[i][j] == target) {
                return true;
            }
            System.out.println("比较过的数据为: " + matrix[i][j]);
            if (matrix[i][j] > target) {
                --j;
            } else {
                ++i;
            }
        }
        return false;
    }
}

